<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Symfony\Component\HttpKernel\Exception\HttpException;

class UserController extends Controller
{

    // 初始化保存模型
    protected function initialize()
    {
        $this->model = new User();
    }

    public function index(Request $request)
    {
        $page = $request->query('page', 1);
        $pageSize = $request->query('pageSize', 10);

        $username = $request->query('username');
        $nickName = $request->query('nickName');
        $email = $request->query('email');
        $phone = $request->query('phone');

        $model  = $this->model;

        if ($username !== null) {
            $model = $model->where('username', 'like', "%{$username}%");
        }

        if ($nickName !== null) {
            $model = $model->where('nickName', 'like', "%{$nickName}%");
        }

        if ($email !== null) {
            $model = $model->where('email', 'like', "%{$email}%");
        }

        if ($phone !== null) {
            $model = $model->where('phone', 'like', "%{$phone}%");
        }

        $model = $model->paginate(
            (int)$pageSize,
            ['*'], // 获取哪些字段，* 表示获取全部
            'page',
            (int)$page
        );

        return $this->successList(list: $model->items(), total: $model->total());
    }

    public function store(Request $request)
    {
        $body = $request->json()->all();

        $body['roles'] = implode(',', $body['roles']);

        $body['password'] = Hash::driver('bcrypt')->make($body['password']);

        try {
            $this->model::create($body);
            return $this->success(data: null);
        } catch (HttpException $error) {
            return $this->error(status: 500, data: $error);
        }
    }

    public function show($id)
    {
        try {
            $model = $this->model->find($id)->toArray();

            $model['roles'] = $model['roles'] ? array_map(function ($item) {
                return (int)$item;
            }, explode(',', $model['roles'])) : null;

            return $this->success(data: $model);
        } catch (HttpException $error) {
            return $this->error(status: 500, data: $error);
        }
    }

    public function update($id, Request $request)
    {
        $body = $request->json()->all();
        $body['roles'] = implode(',', $body['roles']);

        try {
            $this->model::where('id', $id)->update($body);
            return $this->success(data: null);
        } catch (HttpException $error) {
            return $this->error(status: 500, data: $error);
        }
    }

    public function destroy($id)
    {
        try {
            $this->model::destroy($id);
            return $this->success(data: null);
        } catch (HttpException $error) {
            return $this->error(status: 500, data: $error);
        }
    }

    public function batchDelete(Request $request)
    {
        $ids = $request->json('ids');
        try {
            $this->model::destroy($ids);
            return $this->success(data: null);
        } catch (HttpException $error) {
            return $this->error(status: 500, data: $error);
        }
    }
}
